package org.hs.phd.odi.bgsub.features;

import java.awt.geom.Rectangle2D;
import java.util.List;

import org.hs.phd.odi.bgsub.vob.VisualObject;

public class ShapeOverlappingFeatureMatcher implements FeatureMatcher {

	@Override
	public VisualObject findBestMatch(VisualObject searchedObject, List<? extends VisualObject> list) {
		ShapeFeature searchedFeature = (ShapeFeature) searchedObject.getFeatures();
		Rectangle2D searchedObjectBounds = searchedFeature.getShape().getBounds2D();		
		
		double maxIntersectionArea = 0;
		VisualObject bestMatch = null;
		
		for(VisualObject obj : list){
			ShapeFeature objFeature = (ShapeFeature) obj.getFeatures();
			
			Rectangle2D objBounds = objFeature.getShape().getBounds2D();

			if( searchedObjectBounds.intersects(objBounds) ){
				Rectangle2D intersection = new Rectangle2D.Double();
				Rectangle2D.intersect(searchedObjectBounds, objBounds, intersection);
				
				double area = intersection.getWidth()*intersection.getHeight();
				
				if(area > maxIntersectionArea){
					maxIntersectionArea = area;
					bestMatch = obj;
				}
			}
			
		}
		
		return bestMatch;
		
	}

}
